<script lang="ts" setup>
import { inject, onMounted, ref } from 'vue'
import type { Ref } from 'vue'
import type { TabPaneName } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import List from '../components/List.vue'
const http: any = inject('http');
const router = useRouter();

const token = ref(window.localStorage.getItem('token') || '');

interface IDataModal {
    id: string;
    title: string;
    author: string;
    content: string;
    cover: string;
    type: number;
    status: number;
    createTime: string;
    recentReadingTime: number;
}
interface IResultModal {
    code: number;
    data: IDataModal[];
    msg: string;
}

const loading: Ref<boolean> = ref(true);
const data: Ref<IDataModal[] | null> = ref(null);
const recentReadingData: Ref<IDataModal[] | null> = ref(null);
const activeName = ref(1);
const getData = (type: string | number) => {
    http.post('/api/article/getArticleList', {
        status: 2,
        type,
    }).then((res: IResultModal) => {
        loading.value = false;
        if (res.code === 1) {
            data.value = res.data;
        }
    })
}
const getrecentReadingList = () => {
    http.get('/api/article/recentReadingList').then((res: IResultModal) => {
        console.log(res);
        if (res.code === 1) {
            recentReadingData.value = res.data;
        }
    })
}
onMounted(() => {
    getData(activeName.value);
    getrecentReadingList();
})


const handleChange = (name: TabPaneName) => {
    getData(name);
}

const goToEdit = () => {
    router.push('/edit');
}

const handleClick = (item: IDataModal) => {
    router.push({
        path: '/articleContent',
        query: {
            id: item.id
        }
    })
}
const goToContent = (item: IDataModal) => {
    router.push({
        path: '/articleContent',
        query: {
            id: item.id
        }
    })
}

</script>
<template>
    <div class="blog">
        <el-row v-if="token">
            <el-col :push="22" :span="2" style="padding-bottom: 10px; text-align: right;">
                <el-button type="primary" size="large" plain @click="goToEdit">写文章</el-button>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <el-col :span="16">
                <el-card style="max-width: 100%">
                    <el-tabs v-model="activeName" class="tabs" @tab-change="handleChange">
                        <el-tab-pane label="代码人生" :name="1">
                            <List :listArr="data" @handleClick="handleClick"></List>
                        </el-tab-pane>
                        <el-tab-pane label="生活日常" :name="2">
                            <List :listArr="data" @handleClick="handleClick"></List>
                        </el-tab-pane>
                        <el-tab-pane label="小说畅想" :name="3">
                            <List :listArr="data" @handleClick="handleClick"></List>
                        </el-tab-pane>
                        <el-tab-pane label="其他" :name="4">
                            <List :listArr="data" @handleClick="handleClick"></List>
                        </el-tab-pane>
                    </el-tabs>
                </el-card>
            </el-col>
            <el-col :span="8">
                <el-card style="max-width: 100%">
                    <div class="hot-list-title">最近浏览</div>
                    <p v-for="(item, index) in recentReadingData" :key="index" class="hot-list-item"
                        @click="goToContent(item)">
                        <span class="hot-list-item-title">{{ index + 1 }}</span>
                        <span class="hot-list-item-content">{{ item.title }}</span>
                    </p>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>
<style scoped>
.blog .tabs>.el-tabs__content {
    padding: 32px;
    color: #6b778c;
    font-size: 32px;
    font-weight: 600;
}

.hot-list-title {
    font-weight: 600;
    font-size: 16px;
    line-height: 2rem;
    letter-spacing: 0.12em;
    padding-bottom: 8px;
    border-bottom: 1px solid #eee;
}

.hot-list-item {
    display: flex;
    padding: 15px 5px;
    cursor: pointer;
    border-radius: 5px;
}

.hot-list-item:hover {
    background-color: #c6e2ff;
    color: #fff;
}

.hot-list-item-title {
    font-weight: 800;
    min-width: 18px;
    line-height: 22px;
    margin: 0 .92rem 0 0;
    color: var(--juejin-font-3);
    font-family: Archivo;
    text-align: left;
    font-size: 15px;
}

.hot-list-item-content {
    color: var(--juejin-font-1);
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
    flex: 1;
    font-size: 15px;
    font-family: Archivo;
}
</style>